package org.king.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import org.king.entity.vo.ActivityEntity;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ResourceBundle;

@Controller
public class AddOrModifyController implements Initializable {
    //被新增或修改的对象
    private ActivityEntity activity = null;
    //引用主控制器，以便回调它的方法
    private MainController mainController;

    @FXML
    private TextField name;
    @FXML
    private TextField description;
    @FXML
    private TextField address;
    @FXML
    private DatePicker beginDate;
    @FXML
    private DatePicker endDate;
    @FXML
    private Spinner<Integer> beginHour;
    @FXML
    private Spinner<Integer> beginMinute;
    @FXML
    private Spinner<Integer> endHour;
    @FXML
    private Spinner<Integer> endMinute;
    @FXML
    private Button btnOk;
    @FXML
    private Button btnCancel;

    public void setActivity(ActivityEntity act) {
        this.activity = act;
        initDataBind();
    }

    public MainController getMainController() {
        return mainController;
    }

    public void setMainController(MainController mainController) {
        this.mainController = mainController;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //初始化时间和分钟
        initializeSpinners();
        btnOk.setOnAction(event -> {
            updateActivityTime();
            mainController.onAddOrEditActivity(activity.getId()==0,activity);
        });
        btnCancel.setOnAction(event -> {
            mainController.onCancelActivity();
        });
    }
    //更新activity的时间
    private void updateActivityTime() {
        activity.setBeginTime(combineDateTime(beginDate, beginHour, beginMinute));
        activity.setEndTime(combineDateTime(endDate, endHour, endMinute));
    }
    private LocalDateTime combineDateTime(DatePicker datePicker, Spinner<Integer> hourSpinner, Spinner<Integer> minuteSpinner) {
        if (datePicker.getValue() == null) {
            return null;
        }
        return LocalDateTime.of(
                datePicker.getValue(),
                LocalTime.of(hourSpinner.getValue(), minuteSpinner.getValue())
        );
    }
    //绑定数据源
    private void initDataBind(){
        if(activity != null){
            name.textProperty().bindBidirectional(activity.nameProperty());
            description.textProperty().bindBidirectional(activity.descriptionProperty());
            address.textProperty().bindBidirectional(activity.addressProperty());
            // 初始化时间控件
            if (activity.getBeginTime() != null) {
                beginDate.setValue(activity.getBeginTime().toLocalDate());
                beginHour.getValueFactory().setValue(activity.getBeginTime().getHour());
                beginMinute.getValueFactory().setValue(activity.getBeginTime().getMinute());
            }
            if (activity.getEndTime() != null) {
                endDate.setValue(activity.getEndTime().toLocalDate());
                endHour.getValueFactory().setValue(activity.getEndTime().getHour());
                endMinute.getValueFactory().setValue(activity.getEndTime().getMinute());
            }
        }
    }
    //初始化时间
    private void initializeSpinners() {
        // 配置小时Spinner (0-23, 默认12, 步长1)
        SpinnerValueFactory.IntegerSpinnerValueFactory hourFactory1 =
                new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 23, 12, 1);
        beginHour.setValueFactory(hourFactory1);
        beginHour.setEditable(true);//允许手动输入
        SpinnerValueFactory.IntegerSpinnerValueFactory hourFactory2 =
                new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 23, 12, 1);
        endHour.setValueFactory(hourFactory2);
        endHour.setEditable(true);

        SpinnerValueFactory.IntegerSpinnerValueFactory minuteFactory1 =
                new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 59, 30, 5);
        beginMinute.setValueFactory(minuteFactory1);
        beginMinute.setEditable(true);
        SpinnerValueFactory.IntegerSpinnerValueFactory minuteFactory2 =
                new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 59, 30, 5);
        endMinute.setValueFactory(minuteFactory2);
        endMinute.setEditable(true);
    }
}
